﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Tezzanator.Net.XNAGUI;
using Tezzanator.Net.XNAGUI.Controls;

namespace OpenST.GUI
{
    class BugMixer
    {
        private const int MixerHeight = 128;
        private const int ChannelWidth = 64;

        Game game;
        Composition composition;

        bool PanelOpen = false;

        private PictureButton MixerBackground; // The background is a button, simply because it will catch the mouse.
        private PictureButton AddBugButton;
        private BaseControl MixerControlsRoot; // The root of all the Mixer controls, Makes removing and re-adding them easy.
        private TextButton mixerpanelopenbutton;
        private Texture2D MixerChannelBackground;
        private SpriteBatch spritebatch;

        private PropertyWindow BugProps;

        private AddBugWindow Addbugwindow;
       
        public BugMixer(Game thegame, Composition thecomposition)
        {
            game = thegame;
            composition = thecomposition;
        }

        public void LoadContent(ContentManager content)
        {
            spritebatch = new SpriteBatch(game.GraphicsDevice);
            
            MixerChannelBackground = content.Load<Texture2D>("GUI Graphics/MixerChannelBack");
            Addbugwindow = new AddBugWindow(game);
            BugProps = new PropertyWindow();

            // Open/Close Button
            mixerpanelopenbutton = new TextButton("Mixer", content.Load<SpriteFont>("Fonts/SongTitleFont"), content.Load<Texture2D>("GUI Graphics/PanelTop"));
            mixerpanelopenbutton.Size = new Vector2(game.GraphicsDevice.Viewport.Width, 24);
            mixerpanelopenbutton.Position = new Vector2(0, game.GraphicsDevice.Viewport.Height - (48 + 24));
            mixerpanelopenbutton.BackgroundColorModifier = new Color(120,100, 120, 240);
            mixerpanelopenbutton.OnClick += new BaseControl.ControlEventHandler(mixerpanelopenbutton_OnClick);
            OpenST.GetComponent<Gui>(game).RootControl.AddChild(mixerpanelopenbutton);

            // Panel background
            MixerBackground = new PictureButton(content.Load<Texture2D>("GUI Graphics/PanelBottom"));
            MixerBackground.Size = new Vector2(game.GraphicsDevice.Viewport.Width, MixerHeight);
            MixerBackground.ColorModifier = new Color(120, 100, 120, 240);
            MixerBackground.Position = new Vector2(0, (game.GraphicsDevice.Viewport.Height - (48)) - MixerHeight); // Put the panel back outside the view area
            MixerBackground.Visible = false;
            OpenST.GetComponent<Gui>(game).RootControl.AddChild(MixerBackground);

            // Add Bug Button
            AddBugButton = new PictureButton(content.Load<Texture2D>("GUI Graphics/AddBugPlus"));
            AddBugButton.Size = new Vector2(40, 40);
            AddBugButton.OnClick += new BaseControl.ControlEventHandler(AddBugButton_OnClick);
            AddBugButton.Visible = false;
            OpenST.GetComponent<Gui>(game).RootControl.AddChild(AddBugButton);

            Addbugwindow.AddButton.OnClick += new BaseControl.ControlEventHandler(AddButton_OnClick);

            MixerControlsRoot = new BaseControl();
            OpenST.GetComponent<Gui>(game).RootControl.AddChild(MixerControlsRoot);
        }

        void AddButton_OnClick(BaseControl Sender)
        {
            // TODO: Find out how to index Dictionary
            ListBox LB = Addbugwindow.BugList;
            List<string> stringlist = new List<string>();
            foreach (KeyValuePair<string, object> item in LB.List)
            {
                stringlist.Add((string)item.Value);
            }


            composition.AddBug(stringlist[LB.SelectedIndex]);

            Addbugwindow.Hide();
            //throw new NotImplementedException();


            DoOpen();
        }

        void AddBugButton_OnClick(BaseControl Sender)
        {
            //  TODO: Add Bug Window?
            Addbugwindow.Show();
            
        }

        void DoOpen()
        {
            // Calculate the position of the Add Button...
            int Channum = composition.Bugs.Count;
            AddBugButton.Position = new Vector2(
                ((Channum * ChannelWidth) + (ChannelWidth / 2)) - 20,
                ((game.GraphicsDevice.Viewport.Height - (48)) - (MixerHeight / 2)) - 20);

            // Clear all the controls
            MixerControlsRoot.Children.Clear();

            // Re-Add them all.
            int x = 0;
            int i = 0;
            foreach (Bug b in composition.Bugs)
            {
                // Remove button
                PictureButton pbdel = new PictureButton(game.Content.Load<Texture2D>("GUI Graphics/delete"), "del" + i);
                pbdel.OnClick += new BaseControl.ControlEventHandler(DelButtonClick);
                pbdel.Position = new Vector2(x + 8, (game.GraphicsDevice.Viewport.Height - (48 + 24)));
                MixerControlsRoot.AddChild(pbdel);

                // Edit button
                PictureButton pbedit = new PictureButton(game.Content.Load<Texture2D>("GUI Graphics/Gnome/Gnome-EditName"), "edit" + i);
                pbedit.OnClick += new BaseControl.ControlEventHandler(pbedit_OnClick);
                pbedit.Position = new Vector2(x + 28, (game.GraphicsDevice.Viewport.Height - (48 + 24)) - 8);
                pbedit.Size = pbedit.Size / 2;
                MixerControlsRoot.AddChild(pbedit);

                x += ChannelWidth;
                i++;
            }

            mixerpanelopenbutton.Size = new Vector2(game.GraphicsDevice.Viewport.Width, 24);
            mixerpanelopenbutton.Position = new Vector2(0, (game.GraphicsDevice.Viewport.Height - (48 + 24)) - MixerHeight);


            PanelOpen = true;

            MixerBackground.Visible = true;
            AddBugButton.Visible = true;
            MixerControlsRoot.Visible = true;
        }

        void pbedit_OnClick(BaseControl Sender)
        {
            if (Sender.Name.StartsWith("edit"))
            {
                int i = Convert.ToInt32(Sender.Name.TrimStart(new char[] { 'e', 'd', 'i', 't' }));

                BugProps.Text = "Bug Properties";
                BugProps.Properties.SelectedObject = composition.Bugs[i];
                BugProps.Show();
                //DoClose();
            }
        }

        void DelButtonClick(BaseControl Sender)
        {
            if (Sender.Name.StartsWith("del"))
            {
                int i = Convert.ToInt32(Sender.Name.TrimStart(new char[] { 'd', 'e', 'l' }));

                composition.Bugs.RemoveAt(i);
                DoClose();
            }
        }

        void DoClose()
        {
            PanelOpen = false;

            MixerBackground.Visible = false;
            AddBugButton.Visible = false;
            MixerControlsRoot.Visible = false;

            mixerpanelopenbutton.Size = new Vector2(game.GraphicsDevice.Viewport.Width, 24);
            mixerpanelopenbutton.Position = new Vector2(0, game.GraphicsDevice.Viewport.Height - (48 + 24));

            //MixerControlsRoot.Children.Clear();
        }

        void mixerpanelopenbutton_OnClick(BaseControl Sender)
        {
            switch (PanelOpen)
            {
                case true:
                    DoClose();
                    break;
                case false:
                    DoOpen();
                    break;
            }
        }

        public void Update(GameTime gameTime)
        {
            if (!PanelOpen)
                return;

        }

        public void Draw(GameTime gameTime)
        {
            if (!PanelOpen)
                return;

            spritebatch.Begin();

            int Channum = 0;
            foreach (Bug bug in composition.Bugs)
            {
                // Draw the channel background...
                spritebatch.Draw(MixerChannelBackground,  new Rectangle(
                Channum * ChannelWidth,
                (game.GraphicsDevice.Viewport.Height - (48)) - MixerHeight,
                ChannelWidth,
                MixerHeight),
                Color.White);

                // Draw the bug sprite on the channel...
                spritebatch.Draw(bug.BugSprite, new Rectangle(
                ((Channum * ChannelWidth) + (ChannelWidth  / 2)) - 8,
                ((game.GraphicsDevice.Viewport.Height - (48)) - MixerHeight) + 8,
                16,
                16),
                Color.White);


                Channum++;
            }

            // Draw the "add bug" channel
            // Draw the channel background...
            spritebatch.Draw(MixerChannelBackground, new Rectangle(
            Channum * ChannelWidth,
            (game.GraphicsDevice.Viewport.Height - (48)) - MixerHeight,
            ChannelWidth,
            MixerHeight),
            Color.White);


            
            
            spritebatch.End();
        }
    }
}
